package com.oak.broke.model.vip.base;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author tanghan
 * @Description 请求统一返回
 * @date 2023/04/14/ 15:31:00
 */
@RestControllerAdvice
public class BrokeVipResponseAdvice implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    /**
     * 参数响应组装
     *
     * @param body
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        // swagger返回json无需包装
        if (request.getURI().getPath().contains("/swagger-resources")
                || request.getURI().getPath().contains("/v2/api-docs") || request.getURI().getPath().startsWith("/file/upload")
                || request.getURI().getPath().contains("/zft/")) {
            return body;
        }

        if (body instanceof byte[]) {
            return body;
        } else if (body instanceof ResponseMessage) {
            return body;
        } else {
            return ResponseMessage.success(body);
        }
    }
}
